package com.arethusa;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import java.awt.ComponentOrientation;
import java.awt.GridBagConstraints;
import javax.swing.JTextField;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

import simpleorm.core.SConnection;
import simpleorm.core.SPreparedStatement;
import simpleorm.core.SResultSet;
import simpleorm.core.SValidationException;

import com.arethusa.langebaandb.Programma;
import com.arethusa.langebaandb.Programmanummers;
import com.arethusa.langebaandb.Start;
import com.arethusa.langebaandb.StartBean;
import com.ibm.j2x.swing.util.TableUtilities;
import com.ibm.j2x.util.CollectionUtilities;
import java.awt.Insets;

public class WijzigenStart extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel jPanel = null;
	private JPanel jPanel1 = null;
	private JLabel jLabel = null;
	private JTextField startNrTextField = null;
	private JLabel jLabel1 = null;
	private JTextField naamTextField = null;
	private JLabel jLabel3 = null;
	private JPanel jPanel2 = null;
	private JTextField capNrTextField = null;
	private JLabel jLabel4 = null;
	private JCheckBox vastCheckBox = null;
	private JPanel jPanel3 = null;
	private JButton wijzigButton = null;
	private JPanel jPanel4 = null;
	private JLabel jLabel2 = null;
	private JComboBox programmaComboBox = null;
	private JLabel jLabel5 = null;
	private JTextField depotNrTextField = null;
	private JTextField verenigingTextField = null;
	private JTextField capKleurTextField = null;
	private JScrollPane jScrollPane = null;
	private JTable startsTable = null;
	private List<StartBean> starts;
	private Long currentID = null;
	private int programmaNr;
	private JLabel jLabel6 = null;
	private JLabel jLabel7 = null;
	private JTextField eindtijdTextField = null;
	private JTextField disTextField = null;
	private String sortBy = "ProgNr,CapNr";
	private Boolean sortAscending = new Boolean(true);
	private WijzigenStart parent = this;
	private JComboBox suffixComboBox = null;

	private class ProgComboActionListener implements ActionListener {

		public void actionPerformed(ActionEvent arg0) {
	        try {
				JComboBox cb = (JComboBox)arg0.getSource();
				String pnr = ((String)cb.getSelectedItem()).substring(0,2);
				programmaNr = (pnr.equalsIgnoreCase("<K")?0:new Integer(pnr).intValue());
				suffixComboBox.removeAllItems();
				boolean hasBegun = SConnection.getConnection().hasBegun();
				if( ! hasBegun ) { SConnection.begin(); }
				SPreparedStatement stmt = Programma.meta.select(
						"PROGNR="+programmaNr, "ProgNr, Suffix");
				SResultSet res = stmt.execute();
				while (res.hasNext()) {
					Programma prg = (Programma)res.getRecord();
					suffixComboBox.addItem(prg.get_fldSuffix());
				}
				if( ! hasBegun ) { SConnection.commit(); }
				fillTable(sortBy, sortAscending);
				clearInvoer();
			} catch (NumberFormatException e) {
				e.printStackTrace();
			}
		}
	}
	
	private class WijzigButtonActionListener implements ActionListener {
		public void actionPerformed(ActionEvent e) {
			StartBean cur = new StartBean();
			cur.setId(currentID);
			cur.setDepotnr(depotNrTextField.getText());
			cur.setVereniging(verenigingTextField.getText());
			cur.setStartnr(startNrTextField.getText());
			cur.setNaam(naamTextField.getText());
			String prg = (String)programmaComboBox.getSelectedItem();
			cur.setPrognr(new Integer(prg.substring(0,2)));
			cur.setSuffix((String)suffixComboBox.getSelectedItem());
			cur.setVast(new Boolean(vastCheckBox.isSelected()));
			cur.setCapkleur(capKleurTextField.getText());
			String nwTijd="";
			try {
				nwTijd = CheckTijd(eindtijdTextField.getText());
			} catch (TijdException e3) {
				JOptionPane.showMessageDialog(null,
						e3.getMessage(),
						"Validatie", JOptionPane.ERROR_MESSAGE);
			}
			cur.setEindtijd(nwTijd);
			cur.setDis(disTextField.getText());
			try {
				cur.setCapnr(new Integer(capNrTextField.getText()));
				cur.update();
				fillTable(sortBy, sortAscending);
				clearInvoer();
			} catch (SValidationException e1) {
				JOptionPane.showMessageDialog(null,
					e1.getMessage(),
					"Validatie", JOptionPane.ERROR_MESSAGE);
			} catch (NumberFormatException e2) {
				JOptionPane.showMessageDialog(null,
					"CapNr moet een nummer zijn.",
					"Validatie", JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
	private class TableSelectionListener implements ListSelectionListener {
	    public void valueChanged(ListSelectionEvent e) {
	        //Ignore extra messages.
	        if (e.getValueIsAdjusting()) return;

	        ListSelectionModel lsm = (ListSelectionModel)e.getSource();
	        if (lsm.isSelectionEmpty()) {
	            //no rows are selected
	        	getWijzigButton().setEnabled(false);
	        } else {
	            int selectedRow = lsm.getMinSelectionIndex();
	            //selectedRow is selected
	        	getWijzigButton().setEnabled(true);
	        	StartBean r = (StartBean)starts.get(selectedRow);
	        	currentID = r.getId();
	    		getSuffixComboBox(). setSelectedItem(r.getSuffix());
	    		getCapKleurTextField().setText(r.getCapkleur());
	    		getCapNrTextField().setText(r.getCapnr().toString());
	    		getVastCheckBox().setSelected(r.getVast().booleanValue());
	        	getStartNrTextField().setText(r.getStartnr());
	        	getNaamTextField().setText(r.getNaam());
	    		getDepotNrTextField().setText(r.getDepotnr());
	    		getVerenigingTextField().setText(r.getVereniging());
	    		getEindtijdTextField().setText(r.getEindtijd());
	    		getDisTextField().setText(r.getDis());
	        }
	    }
	}
	
	private class TableSorter extends MouseAdapter {
        public void mouseClicked(MouseEvent e) {
            TableColumnModel columnModel = startsTable.getColumnModel();
            int viewColumn = columnModel.getColumnIndexAtX(e.getX()); 
            int column = startsTable.convertColumnIndexToModel(viewColumn); 
            if(e.getClickCount() == 1 && column != -1) {
            	if( sortBy.equalsIgnoreCase(startsTable.getModel().getColumnName(column))) {
            		sortAscending = new Boolean(!sortAscending.booleanValue());
            	} else {
            		sortAscending = new Boolean(true);
            	}
                sortBy = startsTable.getModel().getColumnName(column);
                fillTable(sortBy, sortAscending);
                clearInvoer();
            }
         }
	}

	/**
	 * This is the default constructor
	 */
	public WijzigenStart() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(419, 521);
		this.add(getJPanel(), java.awt.BorderLayout.NORTH);
		this.add(getJScrollPane(), java.awt.BorderLayout.CENTER);
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jPanel = new JPanel();
			jPanel.setLayout(new BorderLayout());
			jPanel.add(getJPanel1(), java.awt.BorderLayout.WEST);
			jPanel.add(getJPanel3(), java.awt.BorderLayout.CENTER);
			jPanel.add(getJPanel4(), java.awt.BorderLayout.NORTH);
		}
		return jPanel;
	}

	/**
	 * This method initializes jPanel1	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel1() {
		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
			gridBagConstraints12.fill = GridBagConstraints.NONE;
			gridBagConstraints12.gridy = 0;
			gridBagConstraints12.weightx = 1.0;
			gridBagConstraints12.anchor = GridBagConstraints.WEST;
			gridBagConstraints12.insets = new Insets(0, 3, 0, 3);
			gridBagConstraints12.gridx = 1;
			GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
			gridBagConstraints11.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints11.gridy = 5;
			gridBagConstraints11.weightx = 1.0;
			gridBagConstraints11.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints11.ipadx = 20;
			gridBagConstraints11.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints11.gridx = 2;
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints10.gridy = 5;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints10.ipadx = 50;
			gridBagConstraints10.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints10.gridx = 1;
			GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
			gridBagConstraints9.gridx = 0;
			gridBagConstraints9.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints9.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints9.gridy = 5;
			jLabel7 = new JLabel();
			jLabel7.setText("Eindtijd");
			GridBagConstraints gridBagConstraints71 = new GridBagConstraints();
			gridBagConstraints71.gridx = 0;
			gridBagConstraints71.insets = new java.awt.Insets(3,3,3,3);
			gridBagConstraints71.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints71.gridy = 0;
			jLabel6 = new JLabel();
			jLabel6.setText("Suffix");
			GridBagConstraints gridBagConstraints61 = new GridBagConstraints();
			gridBagConstraints61.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints61.gridy = 4;
			gridBagConstraints61.weightx = 1.0;
			gridBagConstraints61.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints61.ipadx = 50;
			gridBagConstraints61.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints61.gridx = 2;
			GridBagConstraints gridBagConstraints51 = new GridBagConstraints();
			gridBagConstraints51.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints51.gridx = 1;
			gridBagConstraints51.gridy = 4;
			gridBagConstraints51.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints51.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints51.ipadx = 20;
			gridBagConstraints51.weightx = 1.0;
			GridBagConstraints gridBagConstraints41 = new GridBagConstraints();
			gridBagConstraints41.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints41.gridy = 3;
			gridBagConstraints41.weightx = 1.0;
			gridBagConstraints41.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints41.gridwidth = 2;
			gridBagConstraints41.gridx = 2;
			GridBagConstraints gridBagConstraints31 = new GridBagConstraints();
			gridBagConstraints31.fill = java.awt.GridBagConstraints.NONE;
			gridBagConstraints31.gridy = 3;
			gridBagConstraints31.weightx = 1.0;
			gridBagConstraints31.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints31.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints31.ipadx = 50;
			gridBagConstraints31.gridx = 1;
			GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
			gridBagConstraints21.gridx = 0;
			gridBagConstraints21.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints21.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints21.fill = java.awt.GridBagConstraints.BOTH;
			gridBagConstraints21.gridy = 3;
			jLabel5 = new JLabel();
			jLabel5.setText("Vereniging");
			jLabel5.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT);
			GridBagConstraints gridBagConstraints7 = new GridBagConstraints();
			gridBagConstraints7.anchor = GridBagConstraints.WEST;
			gridBagConstraints7.gridx = 3;
			gridBagConstraints7.gridy = 4;
			gridBagConstraints7.fill = GridBagConstraints.VERTICAL;
			GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
			gridBagConstraints6.anchor = GridBagConstraints.EAST;
			gridBagConstraints6.gridx = 0;
			gridBagConstraints6.gridy = 4;
			gridBagConstraints6.insets = new java.awt.Insets(0,3,0,3);
			jLabel3 = new JLabel();
			jLabel3.setText("Cap");
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.anchor = GridBagConstraints.WEST;
			gridBagConstraints3.gridx = 1;
			gridBagConstraints3.gridy = 2;
			gridBagConstraints3.ipadx = 200;
			gridBagConstraints3.weightx = 1.0D;
			gridBagConstraints3.gridwidth = 3;
			gridBagConstraints3.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints3.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.anchor = GridBagConstraints.EAST;
			gridBagConstraints2.gridx = 0;
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.ipadx = 50;
			gridBagConstraints2.insets = new java.awt.Insets(0,3,0,3);
			jLabel1 = new JLabel();
			jLabel1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			jLabel1.setText("Naam");
			jLabel1.setName("jLabel2");
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.anchor = GridBagConstraints.WEST;
			gridBagConstraints1.insets = new java.awt.Insets(0,3,0,3);
			gridBagConstraints1.gridx = 1;
			gridBagConstraints1.gridy = 1;
			gridBagConstraints1.ipadx = 70;
			gridBagConstraints1.weightx = 1.0;
			gridBagConstraints1.gridwidth = 2;
			gridBagConstraints1.fill = GridBagConstraints.NONE;
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.anchor = GridBagConstraints.EAST;
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 1;
			gridBagConstraints.ipadx = 10;
			gridBagConstraints.weightx = 3.0D;
			gridBagConstraints.insets = new java.awt.Insets(0,3,0,3);
			jLabel = new JLabel();
			jLabel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
			jLabel.setText("Startnummer");
			jLabel.setName("jLabel1");
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jLabel, gridBagConstraints);
			jPanel1.add(getStartNrTextField(), gridBagConstraints1);
			jPanel1.add(jLabel1, gridBagConstraints2);
			jPanel1.add(getNaamTextField(), gridBagConstraints3);
			jPanel1.add(jLabel3, gridBagConstraints6);
			jPanel1.add(getJPanel2(), gridBagConstraints7);
			jPanel1.add(jLabel5, gridBagConstraints21);
			jPanel1.add(getDepotNrTextField(), gridBagConstraints31);
			jPanel1.add(getVerenigingTextField(), gridBagConstraints41);
			jPanel1.add(getCapNrTextField(), gridBagConstraints51);
			jPanel1.add(getCapKleurTextField(), gridBagConstraints61);
			jPanel1.add(jLabel6, gridBagConstraints71);
			jPanel1.add(jLabel7, gridBagConstraints9);
			jPanel1.add(getEindtijdTextField(), gridBagConstraints10);
			jPanel1.add(getDisTextField(), gridBagConstraints11);
			jPanel1.add(getSuffixComboBox(), gridBagConstraints12);
		}
		return jPanel1;
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getStartNrTextField() {
		if (startNrTextField == null) {
			startNrTextField = new JTextField();
		}
		return startNrTextField;
	}

	/**
	 * This method initializes jTextField1	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getNaamTextField() {
		if (naamTextField == null) {
			naamTextField = new JTextField();
		}
		return naamTextField;
	}

	/**
	 * This method initializes jPanel2	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel2() {
		if (jPanel2 == null) {
			jLabel4 = new JLabel();
			jLabel4.setPreferredSize(new Dimension(80, 16));
			jLabel4.setText("Vaste cap");
			jLabel4.setHorizontalAlignment(SwingConstants.RIGHT);
			jPanel2 = new JPanel();
			jPanel2.setLayout(new BoxLayout(getJPanel2(), BoxLayout.X_AXIS));
			jPanel2.add(jLabel4, null);
			jPanel2.add(getVastCheckBox(), null);
		}
		return jPanel2;
	}

	/**
	 * This method initializes jTextField2	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCapNrTextField() {
		if (capNrTextField == null) {
			capNrTextField = new JTextField();
			capNrTextField.setPreferredSize(new java.awt.Dimension(10,20));
			capNrTextField.setHorizontalAlignment(JTextField.LEFT);
		}
		return capNrTextField;
	}

	/**
	 * This method initializes jCheckBox	
	 * 	
	 * @return javax.swing.JCheckBox	
	 */
	private JCheckBox getVastCheckBox() {
		if (vastCheckBox == null) {
			vastCheckBox = new JCheckBox();
		}
		return vastCheckBox;
	}

	/**
	 * This method initializes jPanel3	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel3() {
		if (jPanel3 == null) {
			jPanel3 = new JPanel();
			jPanel3.setLayout(new BoxLayout(getJPanel3(), BoxLayout.Y_AXIS));
			jPanel3.add(getWijzigButton(), null);
		}
		return jPanel3;
	}

	/**
	 * This method initializes jButton1	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getWijzigButton() {
		if (wijzigButton == null) {
			wijzigButton = new JButton();
			wijzigButton.setEnabled(false);
			wijzigButton.setHorizontalAlignment(SwingConstants.LEFT);
			wijzigButton.setText("Wijzig");
			wijzigButton.setPreferredSize(new Dimension(70, 20));
			wijzigButton.addActionListener(new WijzigButtonActionListener());
		}
		return wijzigButton;
	}

	/**
	 * This method initializes jPanel4	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel4() {
		if (jPanel4 == null) {
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.fill = java.awt.GridBagConstraints.HORIZONTAL;
			gridBagConstraints5.gridx = 1;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.weightx = 1.0;
			gridBagConstraints5.anchor = java.awt.GridBagConstraints.EAST;
			gridBagConstraints5.insets = new java.awt.Insets(5,5,5,5);
			GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
			gridBagConstraints4.insets = new java.awt.Insets(5,5,5,5);
			gridBagConstraints4.gridy = 0;
			gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
			gridBagConstraints4.gridx = 0;
			jLabel2 = new JLabel();
			jLabel2.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
			jLabel2.setPreferredSize(new java.awt.Dimension(80,16));
			jLabel2.setText("Programma");
			jLabel2.setName("jLabel3");
			jPanel4 = new JPanel();
			jPanel4.setLayout(new GridBagLayout());
			jPanel4.setComponentOrientation(java.awt.ComponentOrientation.UNKNOWN);
			jPanel4.add(jLabel2, gridBagConstraints4);
			jPanel4.add(getProgrammaComboBox(), gridBagConstraints5);
		}
		return jPanel4;
	}

	/**
	 * This method initializes programmaComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getProgrammaComboBox() {
		if (programmaComboBox == null) {
			programmaComboBox = new JComboBox();
			programmaComboBox.addActionListener(new ProgComboActionListener());
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			SPreparedStatement stmt = Programmanummers.meta.select(
					"1=1", "ProgNr");
			SResultSet res = stmt.execute();
			programmaComboBox .addItem("<Kies programma>");
			while (res.hasNext()) {
				Programmanummers prg = (Programmanummers)res.getRecord();
				int pnr = prg.get_fldPrognr().intValue();
				programmaComboBox .addItem(
						(pnr<10?"0":"")+pnr+" "+prg.get_fldTitel());
			}
			SConnection.commit();
		}
		return programmaComboBox;
	}

	/**
	 * This method initializes depotNrTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDepotNrTextField() {
		if (depotNrTextField == null) {
			depotNrTextField = new JTextField();
			depotNrTextField.setName("");
		}
		return depotNrTextField;
	}

	/**
	 * This method initializes verenigingTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getVerenigingTextField() {
		if (verenigingTextField == null) {
			verenigingTextField = new JTextField();
		}
		return verenigingTextField;
	}

	/**
	 * This method initializes capKleurTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getCapKleurTextField() {
		if (capKleurTextField == null) {
			capKleurTextField = new JTextField();
		}
		return capKleurTextField;
	}

	/**
	 * This method initializes jScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getStartsTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable	
	 * 	
	 * @return javax.swing.JTable	
	 */
	private JTable getStartsTable() {
		if (startsTable == null) {
			startsTable = new JTable();
//			Ask to be notified of selection changes.
			startsTable.getSelectionModel().addListSelectionListener(new TableSelectionListener());
//			 Make column header click result in sorting ascending (shift-click is descending)
			startsTable.setColumnSelectionAllowed(false);
	        TableSorter listSorter = new TableSorter();
			startsTable.getTableHeader().addMouseListener(listSorter);
		}
		return startsTable;
	}

	@SuppressWarnings("unchecked")
	private void fillTable(String sortBy, Boolean ascending) {
		List<StartBean> tmp = new ArrayList<StartBean>();
		/// Prepare and execute the query
		if( programmaNr > 0 ) {
			boolean hasBegun = SConnection.getConnection().hasBegun();
			if( ! hasBegun ) { SConnection.begin(); }
			SPreparedStatement stmt = Start.meta.select(
					"ProgNr="+programmaNr, sortBy+(ascending.booleanValue()?"":" DESC"));
			SResultSet res = stmt.execute();
			
			/// loop through the results
			while (res.hasNext()) {
				StartBean strt = new StartBean(((Start)res.getRecord()));
				tmp.add(strt);
			}
	    	SConnection.commit();
	    	starts = CollectionUtilities.observableList(tmp);
			TableUtilities.setViewToModel("LangebaanModel.xml", "Start", getStartsTable(), starts);
			TableColumn col1 = getStartsTable().getColumnModel().getColumn(0); //Suffix
			col1.setMaxWidth(10);
			TableColumn col2 = getStartsTable().getColumnModel().getColumn(1); //CapKleur
			col2.setMaxWidth(50);
			TableColumn col3 = getStartsTable().getColumnModel().getColumn(2); //CapNr
			col3.setMaxWidth(30);
			TableColumn col4 = getStartsTable().getColumnModel().getColumn(3); //Vast
			col4.setMaxWidth(40);
			TableColumn col5 = getStartsTable().getColumnModel().getColumn(4); //StartNr
			col5.setMaxWidth(75);
			TableColumn col6 = getStartsTable().getColumnModel().getColumn(5); //Naam
//			col6.setPreferredWidth(150);
			col6.setMaxWidth(150);
			TableColumn col7 = getStartsTable().getColumnModel().getColumn(6); //DepotNr
			col7.setMaxWidth(50);
			TableColumn col8 = getStartsTable().getColumnModel().getColumn(7); //Vereniging
			col8.setMaxWidth(150);
			TableColumn col9 = getStartsTable().getColumnModel().getColumn(8); //Eindtijd
			col9.setMaxWidth(60);
			TableColumn col10 = getStartsTable().getColumnModel().getColumn(9); //Dis
			col10.setMaxWidth(30);
			TableColumn col11 = getStartsTable().getColumnModel().getColumn(10); //EindtijdInSec
			col11.setMaxWidth(50);
		}
	}

	private void clearInvoer() {
		getCapKleurTextField().setText(null);
		getCapNrTextField().setText(null);
		getVastCheckBox().setSelected(false);
		getStartNrTextField().setText(null);
		getNaamTextField().setText(null);
		getDepotNrTextField().setText(null);
		getVerenigingTextField().setText(null);
		getEindtijdTextField().setText(null);
		getDisTextField().setText(null);
	}


	/**
	 * This method initializes eindtijdTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getEindtijdTextField() {
		if (eindtijdTextField == null) {
			eindtijdTextField = new JTextField();
			eindtijdTextField.addFocusListener(new java.awt.event.FocusAdapter() {
				public void focusLost(java.awt.event.FocusEvent e) {
					try {
						CheckTijd(parent.getEindtijdTextField().getText());
					} catch (TijdException e1) {
						JOptionPane.showMessageDialog(null,
								e1.getMessage(),
								"Validatie", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
		}
		return eindtijdTextField;
	}

	/**
	 * This method initializes disTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getDisTextField() {
		if (disTextField == null) {
			disTextField = new JTextField();
		}
		return disTextField;
	}

	/**
	 * This method initializes suffixComboBox	
	 * 	
	 * @return javax.swing.JComboBox	
	 */
	private JComboBox getSuffixComboBox() {
		if (suffixComboBox == null) {
			suffixComboBox = new JComboBox();
			suffixComboBox.setPreferredSize(new Dimension(35, 20));
			suffixComboBox.setMaximumRowCount(5);
		}
		return suffixComboBox;
	}
	
	/*=====================================================================*/
	/**
	 * This method checks and formats the entered time.
	 * the time is entered as a string of digits representing the time in hundreths of seconds.
	 * 
	 * @param	tijd	Time to check and format
	 * @return			Formatted time
	 * @throws	TijdException
	 */
	private String CheckTijd(String tijd ) throws TijdException {
		String nwTijd = tijd;
		
		if( tijd.length()== 0 ) return nwTijd;
		
		if( Helper.isNumber(tijd) ) {
			if( tijd.length() > 6 ) {
				throw new TijdException("Tijd niet juist ingevoerd: teveel cijfers");
			}
			if( tijd.length() < 5 ){
				throw new TijdException("Tijd niet juist ingevoerd: niet voldoende cijfers");
			}
			if( tijd.length() == 5 ) {
				nwTijd = "0" + tijd.substring(0,1)+":"+tijd.substring(1,3)+"."+tijd.substring(3,5);
			} else {
				nwTijd = tijd.substring(0,2)+":"+tijd.substring(2,4)+"."+tijd.substring(4,6);
			}
		} else {
			if(	tijd.equalsIgnoreCase("agmk") ||
				tijd.equalsIgnoreCase("agzk") ||
				tijd.equalsIgnoreCase("na") ) {
				nwTijd = tijd.toLowerCase();
			} else {
				throw new TijdException("5 of 6 cijfers of agmk,agzk of na is toegestaan.");
			}
		}
		return nwTijd;
	}
}  //  @jve:decl-index=0:visual-constraint="10,10"
